package ch.fhnw.qualiscan.application.views;

import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.components.LoginBox;
import ch.fhnw.qualiscan.application.controllers.Invitation;
import ch.fhnw.qualiscan.core.View;
import eu.livotov.tpt.i18n.TM;

public class InvitationView extends View {
	
	private Invitation controller;
	
	public InvitationView(Invitation controller) {
		this.controller = controller;
	}

	@Override
	public void initComponents() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildLayout() {
		if(controller.getInvitationSurvey() != null) {
			
			addComponent(textHelper.h1(TM.get("invitation.title", controller.getInvitationSurvey().getSurvey().getTitle())));
			
			if(controller.userSessionHelper.getUserSession() == null) {
				addComponent(tab1());
			} else {
				addComponent(tab2());
			}
			
		} else {
			addComponent(tab3());
		}
		
	}
	
	// shown when no session active
		public VerticalLayout tab1() {
			VerticalLayout tab1 = new VerticalLayout();

			HorizontalLayout grid = new HorizontalLayout();
			grid.setWidth("100%");
			grid.setStyleName("InvitationTab1Grid");
			grid.setSpacing(true);
			
			// if user does not exist in db
			if(controller.getInvitationSurvey().getUser().getFirstname().equals("")) {
				
				// register
				VerticalLayout newAccountBox = new VerticalLayout();
				newAccountBox.setMargin(true);
				newAccountBox.addComponent(textHelper.h2(TM.get("invitation.register.title")));
				newAccountBox.addComponent(textHelper.p(TM.get("invitation.register.desc")));
				
				Button newAccount = new Button(TM.get("invitation.register.button"));
				newAccount.addListener(new ClickListener() {
					public void buttonClick(ClickEvent event) {
						controller.getAppData().getLoaderLib().setUrl("NewAccount/0/"+controller.getHash());
						
					}
				});
				newAccount.setIcon(Config.ICON_ADDUSER);
				newAccountBox.addComponent(newAccount);
				
				// login box
				VerticalLayout loginBox = new VerticalLayout();
				loginBox.setMargin(true);
				loginBox.addComponent(textHelper.h2(TM.get("invitation.login")));
				LoginBox loginForm = new LoginBox(controller.getInvitationSurvey(), true);
				loginBox.addComponent(loginForm);
				grid.addComponent(loginBox);
				
				grid.addComponent(newAccountBox);
			}
			
			// user does exist already
			else {
				VerticalLayout loginBox = new VerticalLayout();
				loginBox.setWidth("500px");
				loginBox.addComponent(textHelper.h2(TM.get("invitation.login")));
				LoginBox loginForm = new LoginBox();
				//loginForm.setUsername(invitationSurvey.getUser().getEmail());
				loginForm.invitationSurvey = controller.getInvitationSurvey();
				loginBox.addComponent(loginForm);
				grid.addComponent(loginBox);
			}
			
			tab1.addComponent(grid);
			
			return tab1;
		}
		
		// shown when logged in
		public VerticalLayout tab2() {
			VerticalLayout tab2 = new VerticalLayout();
			tab2.addComponent(textHelper.p(TM.get("invitation.loggedin.status", controller.getInvitationSurvey().getSurvey().getTitle())));
			
			HorizontalLayout Buttons = new HorizontalLayout();
			
			Button yesButton = new Button("goto.dashboard");
			yesButton.setIcon(Config.ICON_HOME);
			yesButton.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					controller.getAppData().getLoaderLib().setUrlFragment(0, "Dashboard");
				}
			});
			
			Buttons.addComponent(yesButton);
			
			tab2.addComponent(Buttons);
			return tab2;
		}
		
		// show when user does not have the right to access the survey
		public VerticalLayout tab3() {
			VerticalLayout tab3 = new VerticalLayout();
			tab3.setSpacing(true);
			
			tab3.addComponent(textHelper.h1(TM.get("invitation.closed.title")));
			tab3.addComponent(textHelper.p(TM.get("invitation.closed.desc")));
			
			return tab3;
		}

		@Override
		public void reBuild() {
			// TODO Auto-generated method stub
			
		}

}
